﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;

namespace VisionCloud.Domain.DtoModel.Ms
{
    /// <summary>
    ///     工效分析--整体数据
    /// </summary>
    public class SummaryEfficiencyStatis
    {
        /// <summary>
        ///     序号
        /// </summary>
        public string SeqNo { get; set; }

        /// <summary>
        ///     用户名称
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        ///     昨日测楼层数
        /// </summary>

        public string YesterdayFloors { get; set; }

        /// <summary>
        ///     昨日测面积
        /// </summary>
        public string YesterdayAreas { get; set; }

        /// <summary>
        ///     总测楼层数
        /// </summary>

        public string TotalFloors { get; set; }

        /// <summary>
        ///     总测楼面积
        /// </summary>
        public string TotalAreas { get; set; }

        /// <summary>
        ///     总工作日
        /// </summary>

        public string WorkingDays { get; set; }

        /// <summary>
        ///     账号名
        /// </summary>

        public string UserAccount { get; set; }
    }

    /// <summary>
    ///     日工作效率
    /// </summary>
    public class DailyEfficiencyStatis
    {
        /// <summary>
        ///     日期
        /// </summary>
        public string DateStr { get; set; }

        /// <summary>
        ///     测量人员
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        ///     层数
        /// </summary>
        public string TotalFloors { get; set; }

        /// <summary>
        ///     面积
        /// </summary>
        public string TotalAreas { get; set; }

        /// <summary>
        ///     账号名
        /// </summary>

        public string UserAccount { get; set; }
    }

    /// <summary>
    ///     月工作效率
    /// </summary>
    public class MonthlyEfficiencyStatis
    {
        /// <summary>
        ///     日期
        /// </summary>
        public string DateStr { get; set; }

        /// <summary>
        ///     测量人员
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        ///     层数
        /// </summary>
        public string TotalFloors { get; set; }

        /// <summary>
        ///     面积
        /// </summary>
        public string TotalAreas { get; set; }

        /// <summary>
        ///     账号名
        /// </summary>

        public string UserAccount { get; set; }
    }

    /// <summary>
    ///     总体工效
    /// </summary>
    public class SummayWorkEfficiency
    {
        /// <summary>
        ///     公司名称
        /// </summary>
        [JsonIgnore]
        public string CompanyName { get; set; }

        /// <summary>
        ///     机器总数
        /// </summary>
        public int TotalDevices { get; set; }

        /// <summary>
        ///     测量员数量
        /// </summary>
        public int TotalPeople { get; set; }

        /// <summary>
        ///     总测量面积
        /// </summary>
        public decimal TotalArea { get; set; }

        /// <summary>
        ///     有效总时间
        /// </summary>
        public int TotalWorkingDays { get; set; }

        /// <summary>
        ///     有效总时间
        /// </summary>
        public int TotalDays { get; set; }

        /// <summary>
        ///     工效
        /// </summary>
        public string WorkEfficiency { get; set; }

        /// <summary>
        ///     时效
        /// </summary>
        public string TimeEfficiency { get; set; }

        /// <summary>
        ///     效率：工效×时效
        /// </summary>
        public string Efficiency { get; set; }
    }

    /// <summary>
    ///     各项目工效
    /// </summary>
    public class ProjectWorkEfficiency
    {
        /// <summary>
        ///     项目编号
        /// </summary>
        public string ProjectNo { get; set; }

        /// <summary>
        ///     项目名称
        /// </summary>
        public string ProjectName { get; set; }

        /// <summary>
        ///     项目省份
        /// </summary>
        public string Province { get; set; }

        /// <summary>
        ///     项目城市
        /// </summary>
        public string City { get; set; }

        /// <summary>
        ///     项目区域
        /// </summary>
        public string Town { get; set; }

        /// <summary>
        ///     项目地点
        /// </summary>
        public string ProjectAddress { get; set; }

        /// <summary>
        ///     总测量层数
        /// </summary>
        public int TotalFloors { get; set; }

        /// <summary>
        ///     总测量面积
        /// </summary>
        public decimal TotalArea { get; set; }

        /// <summary>
        ///     项目停留时间
        /// </summary>
        public int TotalDays { get; set; }

        /// <summary>
        ///     区域公司
        /// </summary>
        public string RegionalCompany { get; set; }
    }

    /// <summary>
    ///     用户工效
    /// </summary>
    public class UserWorkEfficiency
    {
        /// <summary>
        ///     序号
        /// </summary>
        public string SeqNo { get; set; }

        /// <summary>
        ///     测量人员
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        ///     总测量层数
        /// </summary>
        public int TotalFloors { get; set; }

        /// <summary>
        ///     总测量面积（㎡）
        /// </summary>
        public decimal TotalArea { get; set; }

        public int TotalWorkingDays { get; set; }

        /// <summary>
        ///     平均工效
        /// </summary>
        public decimal WorkEfficiency { get; set; }

        /// <summary>
        ///     测量人员账号
        /// </summary>
        public string Account { get; set; }
    }

    /// <summary>
    ///     设备工效
    /// </summary>
    public class DeviceEfficiency
    {
        public string DeviceNumber { get; set; }

        /// <summary>
        ///     测量员
        /// </summary>
        public string YunUserName { get; set; }

        public int TotalFloors { get; set; }

        /// <summary>
        ///     总测量面积
        /// </summary>
        public decimal TotalArea { get; set; }

        /// <summary>
        ///     结构初测
        /// </summary>
        public decimal StructureTestAreas { get; set; }

        /// <summary>
        ///     结构复测
        /// </summary>
        public decimal StructureRetestAreas { get; set; }

        /// <summary>
        ///     结构其他
        /// </summary>
        public decimal StructureOtherAreas { get; set; }

        /// <summary>
        ///     砌筑初测
        /// </summary>
        public decimal MasonryTestAreas { get; set; }

        /// <summary>
        ///     砌筑复测
        /// </summary>
        public decimal MasonryRetestAreas { get; set; }

        /// <summary>
        ///     砌筑其他
        /// </summary>
        public decimal MasonryOtherAreas { get; set; }

        /// <summary>
        ///     抹灰初测
        /// </summary>
        public decimal PlasteTestAreas { get; set; }

        /// <summary>
        ///     抹灰复测
        /// </summary>
        public decimal PlasteRetestAreas { get; set; }

        /// <summary>
        ///     抹灰其他
        /// </summary>
        public decimal PlasteOtherAreas { get; set; }

        /// <summary>
        ///     装饰初测
        /// </summary>
        public decimal DecorateTestAreas { get; set; }

        /// <summary>
        ///     装饰复测
        /// </summary>
        public decimal DecorateRetestAreas { get; set; }

        /// <summary>
        ///     装饰其他
        /// </summary>
        public decimal DecorateOtherAreas { get; set; }

        /// <summary>
        ///     总作业时间
        /// </summary>
        public int TotalDays { get; set; }

        /// <summary>
        ///     平均工效
        /// </summary>
        public decimal WorkEfficiency { get; set; }

        /// <summary>
        ///     闲置(日)
        /// </summary>
        public int IdleDay { get; set; }

        /// <summary>
        ///     区域公司
        /// </summary>
        public string RegionalCompany { get; set; }

        /// <summary>
        /// 第一次上传数据日期
        /// </summary>
        public string FirstUploadData { get; set; }
    }

    /// <summary>
    ///     设备工效详情
    /// </summary>
    public class DeviceEfficiencyDetail : IComparable<DeviceEfficiencyDetail>
    {
        public string DeviceNumber { get; set; }

        public string DateStr { get; set; }

        private DateTime _completedDate;

        public DateTime CompletedDate
        {
            get { return _completedDate; }
            set
            {
                _completedDate = value;
                DateStr = value.ToString("yyyy/M/d");
            }
        }

        public decimal TotalArea { get; set; }

        /// <summary>
        ///     项目
        /// </summary>
        public string ProjectName { get; set; }

        /// <summary>
        ///     项目经理
        /// </summary>
        public string ProjectManager { get; set; }

        /// <summary>
        ///     楼栋
        /// </summary>
        public string BuildingNum { get; set; }

        /// <summary>
        ///     楼层
        /// </summary>
        public int? FloorNum { get; set; }

        /// <summary>
        ///     工程阶段
        /// </summary>
        public string ProjectStandardName { get; set; }

        /// <summary>
        ///     自检巡检
        /// </summary>
        public string TaskTypeName { get; set; }

        /// <summary>
        ///     初测复测
        /// </summary>
        public string TaskProperty { get; set; }

        /// <summary>
        ///     测量员
        /// </summary>
        public string YunUserName { get; set; }

        /// <summary>
        ///     区域公司
        /// </summary>
        public string RegionalCompany { get; set; }

        /// <summary>
        /// 只采模式
        /// </summary>
        public string OnlyCollectMode { get; set; } = "否";

        /// <summary>
        ///     实现快速排序。
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public int CompareTo(DeviceEfficiencyDetail other)
        {
            //比other大，大于0；等于other，0；小于other，小于0
            int result;
            if (this?.DeviceNumber == null && other?.DeviceNumber != null) return -1;
            if (this?.DeviceNumber == null && other?.DeviceNumber == null) return 0;
            if (other?.DeviceNumber == null) return 1;
            result = DeviceNumber.CompareTo(other.DeviceNumber);
            if (result == 0) result = CompletedDate.CompareTo(other.CompletedDate);
            return result;
        }
    }

    public static class Extension
    {
        /// <summary>
        ///     为工效详情增加空行(拓展方法)
        /// </summary>
        /// <param name="list">工效详情数据集</param>
        /// <param name="timeConditon">时间条件</param>
        /// <param name="enabledDates">第一次工作时间</param>
        /// <returns>新的工效详情数据集</returns>
        public static void AddNullRowsExt(this List<DeviceEfficiencyDetail> list,
            TimeConditon timeConditon,
            Dictionary<string, DateTime> enabledDates)
        {
            var endDate = DateTime.Parse(timeConditon.EndTime);

            foreach (var group in list.Select(x => new { x.DeviceNumber, x.CompletedDate }).ToLookup(y => y.DeviceNumber))
            {
                var enabledDate = enabledDates[group.Key];
                var startDate = DateTime.Parse(timeConditon.StartTime);
                startDate = startDate >= enabledDate ? startDate : enabledDate;
                for (var current = startDate; current <= endDate; current = current.AddDays(1))
                {
                    if (!group.Any(y => y.CompletedDate == current))
                        list.Add(new DeviceEfficiencyDetail
                        { DeviceNumber = group.Key, CompletedDate = current, TotalArea = 0 });
                }
            }
        }
    }

    public class TimeConditon
    {
        /// <summary>
        /// 搜索开始时间
        /// </summary>
        public string StartTime { get; set; }

        /// <summary>
        /// 搜索结束时间
        /// </summary>
        public string EndTime { get; set; }

        /// <summary>
        /// 有数据上传起始时间
        /// </summary>
        public string ActualStartTime { get; set; }

        /// <summary>
        /// 最近有数据上传时间
        /// </summary>
        public string AcutalEndTime { get; set; }

        /// <summary>
        /// 类型：
        /// 自检 = 1,
        /// 巡检 = 2,
        /// </summary>
        public List<EnumTaskTypeName> TaskTypeArray { get; set; }

        /// <summary>
        /// 阶段：
        /// 结构初测 = 1,
        /// 结构复测 = 2,
        /// 结构其他 = 64,
        /// 砌筑初测 = 4,
        /// 砌筑复测 = 8,
        /// 砌筑其他 = 128,
        /// 抹灰初测 = 16,
        /// 抹灰复测 = 32,
        /// 抹灰其他 = 256
        /// </summary>
        public List<EnumTaskProperty> TaskPropertyArray { get; set; }
    }

    public class PersonnelEfficiencyDetailInfo : DeviceEfficiencyDetail
    {
        /// <summary>
        /// 项目编号
        /// </summary>
        public string ProjectNo { get; set; }

        /// <summary>
        ///     项目省份
        /// </summary>
        public string Province { get; set; }

        /// <summary>
        ///     项目城市
        /// </summary>
        public string City { get; set; }

        /// <summary>
        ///     项目区域
        /// </summary>
        public string Town { get; set; }

        /// <summary>
        ///     项目地点
        /// </summary>
        public string ProjectAddress { get; set; }

        /// <summary>
        ///     测量人员账号
        /// </summary>
        public string Account { get; set; }
    }
}